/**
 * Copyright 2007 The Apache Software Foundation
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package libMapReduce;

import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.conf.Configurable;
import org.apache.hadoop.conf.Configuration;

/**
 * Convert HBase tabular data from multiple input tables and scanners into a
 * format that is consumable by Map/Reduce.
 * 
 * <p>
 * Usage example
 * </p>
 * 
 * <pre>
 * Scan scan1 = new Scan();
 * scan1.setStartRow(firstRow1);
 * scan1.setStopRow(lastRow1);
 * 
 * Scan scan2 = new Scan();
 * scan2.setStartRow(firstRow2);
 * scan2.setStopRow(lastRow2);
 * 
 * MultiTableInputCollection mtic = new MultiTableInputCollection();
 * mtic.Add(tableName1, scan1);
 * mtic.Add(tableName2, scan2);
 * 
 * TableMapReduceUtil.initTableMapperJob(mtic, TableMapper.class, Text.class,
 *     IntWritable.class, job);
 * </pre>
 */
public class MultiTableInputFormat extends MultiTableInputFormatBase implements
    Configurable {

  private final Log LOG = LogFactory.getLog(MultiTableInputFormat.class);

  /** Job parameter that specifies the input tables list. */
  public static final String INPUT_TABLES = "hbase.mapreduce.inputtables";

  /** The configuration. */
  private Configuration conf = null;

  /**
   * Returns the current configuration.
   * 
   * @return The current configuration.
   * @see org.apache.hadoop.conf.Configurable#getConf()
   */
  @Override
  public Configuration getConf() {
    return conf;
  }

  /**
   * Sets the configuration. This is used to set the details for the table to be
   * scanned.
   * 
   * @param configuration The configuration to set.
   * @see org.apache.hadoop.conf.Configurable#setConf(org.apache.hadoop.conf.Configuration)
   */
  @Override
  public void setConf(Configuration configuration) {
    this.conf = configuration;
    String[] inputTables = conf.getStrings(INPUT_TABLES);
    MultiTableInputCollection tc = new MultiTableInputCollection();

    for (int i = 0; i < inputTables.length; i = i + 2) {
      try {
        tc.Add(inputTables[i],
            TableMapReduceUtil.convertStringToScan(inputTables[i + 1]));
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    this.setInputTables(tc);
  }
}